package com.scalar.forecast.salesForecastEntry.actions;

import com.scalar.forecast.ForecastExtendedAction;
import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.salesForecastEntry.forms.SalesForecastEntryForm;
import com.scalar.forecast.salesForecastEntry.actionCommands.SalesForecastEntryActionCommandFactory;
import com.scalar.forecast.commons.SessionKeys;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * User: Sujan
 * Date: Jul 13, 2005
 * Time: 4:40:45 PM
 */
public class SalesForecastEntryAction extends ForecastExtendedAction
{
    public ActionForward executeRequest(ActionMapping mapping,
                                        ActionForm form,
                                        HttpServletRequest request,
                                        HttpServletResponse response) throws Exception
    {
        ActionForward actionForward = mapping.findForward ( "salesForecastEntrySearch" );
        String action = request.getParameter ( "action" );
        HashMap errors = new HashMap();
        HashMap info = new HashMap();
        HttpSession session = request.getSession();
        String salesPersonId = ( String ) session.getAttribute( USER_SALESPERSON_ID );
        //IF USER DON'T HAVE THE PERMISSION 'Forecast Entry For All Sales Persons' THEN CHECK FOR THE SALES PERSON
        if ( !pUser.isActionAllowed( Functionality.FORECAST_ENTRY_FOR_ALL_SALES_PERSONS, User.USER_ACTION_MODIFY ) && ( salesPersonId == null || salesPersonId.trim().length() == 0 ) )
        {
            errors.put ( "Only Sales Persons can enter Sales Forecast", null );
            request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
            return mapping.findForward ( "blankPage" );
        }
        /*else if ( !ForecastEntryDAO.isSlesPersonLevelValid( null, salesPersonId ) )
        {
            info.put ( INFO_SALESPERSON_LEVEL_NOT_MATCHED, "" );
            request.setAttribute ( INFO_MESSAGES, info );
            return mapping.findForward( "blankPage" );
        }*/
        try
        {
            if ( action == null )
            {
                action = ACTION_SEARCH;
            }
            SalesForecastEntryForm forecastEntryForm = ( SalesForecastEntryForm )form;

            ActionCommand actionCommand = SalesForecastEntryActionCommandFactory.getActionCommand( action );
            if ( actionCommand != null)
            {
                actionForward = actionCommand.execute( mapping, form, request, response );
            }

        }
        catch( Exception e )
        {
            e.printStackTrace();
        }
        return actionForward;
    }
}
